package com.gis.visualization.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gis.visualization.config.exceptionhandler.VisException;
import com.gis.visualization.config.listener.EventExcelListener;
import com.gis.visualization.entity.HistoryEvent;
import com.gis.visualization.entity.vo.EventExcelVo;
import com.gis.visualization.entity.vo.EventQueryVo;
import com.gis.visualization.mapper.HistoryEventMapper;
import com.gis.visualization.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 历史事件表 服务实现类
 * </p>
 *
 * @author hqz
 * @since 2021-02-24
 */
@Service
public class HistoryEventServiceImpl extends ServiceImpl<HistoryEventMapper, HistoryEvent> implements HistoryEventService {

    @Autowired
    private EventDescService eventDescService;

    @Autowired
    private HistorySummaryService summaryService;

    @Override
    public Map<String, Object> pageEventCondition(long current, long limit, EventQueryVo eventQueryVo) {
        //        创建page对象
        Page<HistoryEvent> pageEvent = new Page<>(current, limit);
//        构建条件
        QueryWrapper<HistoryEvent> wrapper = new QueryWrapper<>();
//        多条件组合查询
//        动态sql 判断条件值是否为空，如果不为空 拼接条件
        if (eventQueryVo != null) {
            String eventTitle = eventQueryVo.getEventTitle();
            String city = eventQueryVo.getCity();
            String begin = eventQueryVo.getBegin();
            String end = eventQueryVo.getEnd();
            if (!StringUtils.isEmpty(eventTitle)) {
//            构建条件 模糊查询
                wrapper.like("event_title", eventTitle);
            }
            if (!StringUtils.isEmpty(city)) {
                wrapper.eq("city", city);
            }
//   参数：column需使用数据库中的字段名     gmt_create
            if (!StringUtils.isEmpty(begin)) {
                wrapper.ge("start_time", begin);
            }
            if (!StringUtils.isEmpty(end)) {
                wrapper.le("start_time", end);
            }
        }
        wrapper.orderByDesc("start_time");
//        调用方法实现条件分页查询

        baseMapper.selectMapsPage(pageEvent,wrapper);
        long total = pageEvent.getTotal();//总记录数
        List<HistoryEvent> records = pageEvent.getRecords();//数据list集合

        Map<String,Object> map = new HashMap<>();
        map.put("total",total);
        map.put("records",records);
        return map;
    }

    @Override
    public void deleteEvent(String eventId) {
//        删除事件
        baseMapper.deleteById(eventId);
//        删除描述
        eventDescService.removeById(eventId);
//        删除总结
        summaryService.removeById(eventId);
    }

    @Override
    @Transactional(rollbackFor = VisException.class)
    public void eventFromExcel(MultipartFile file, HistoryEventService eventService,
                               EventDescService eventDescService,
                               HistorySummaryService summaryService,
                               OssService ossService,
                               VideoService videoService) {
        try {
            InputStream fileInputStream = file.getInputStream();
            EventExcelListener eventExcelListener = new EventExcelListener(eventService
            ,eventDescService,summaryService,ossService,videoService);
//            调用excel 的读取
            EasyExcel.read(fileInputStream, EventExcelVo.class,eventExcelListener).sheet().doRead();
        }catch (Exception e){
            e.printStackTrace();
            throw new VisException(20001,"读取excel失败,请检查excel中内容格式是否标准");
        }
    }
}
